package prisd.brand.com.threegangswitch.util;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class WiFiUtil {
    private static final String TAG = "WifiUtils";
    public static final int WIFICIPHER_NOPASS = 0;
    public static final int WIFICIPHER_WEP = 1;
    public static final int WIFICIPHER_WPA = 3;
    private static final int WIFICIPHER_WPA2 = 4;

    public WiFiUtil() {
    }

    public static String intToIp(int i) {
        return (i & 255) + "." + (i >> 8 & 255) + "." + (i >> 16 & 255) + "." + (i >> 24 & 255);
    }

    public static String getSSID(WifiManager wifiManager) {
        WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null) {
            String ssid = connectionInfo.getSSID();
            if (TextUtils.isEmpty(ssid)) {
                return "";
            } else {
                if (Build.VERSION.SDK_INT >= 17 && ssid.startsWith("\"") && ssid.endsWith("\"")) {
                    ssid = ssid.replaceAll("^\"|\"$", "");
                }

                return ssid;
            }
        } else {
            return "";
        }
    }

    public static String getCurrentSSID(Context context) {
        return getCurrentSSID((WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE));
    }

    public static WifiConfiguration IsExsits(WifiManager mainWifi, String SSID) {
        List<WifiConfiguration> existingConfigs = mainWifi.getConfiguredNetworks();
        Iterator var3 = existingConfigs.iterator();

        WifiConfiguration existingConfig;
        do {
            if (!var3.hasNext()) {
                return null;
            }

            existingConfig = (WifiConfiguration)var3.next();
        } while(!existingConfig.SSID.equals("\"" + SSID + "\""));

        return existingConfig;
    }

    public static WifiConfiguration createWifiConfig(WifiManager mainWifi, String SSID, String Password, int Type) {
        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();
        config.priority = 40;
        config.status = 1;
        config.SSID = "\"" + SSID + "\"";
        WifiConfiguration tempConfig = IsExsits(mainWifi, SSID);
        if (tempConfig != null) {
            mainWifi.removeNetwork(tempConfig.networkId);
        }

        if (Type == 0) {
            config.wepKeys[0] = "";
            config.wepTxKeyIndex = 0;
            config.allowedKeyManagement.set(0);
            config.allowedProtocols.set(1);
            config.allowedProtocols.set(0);
            config.allowedPairwiseCiphers.set(2);
            config.allowedPairwiseCiphers.set(1);
            config.allowedGroupCiphers.set(0);
            config.allowedGroupCiphers.set(1);
            config.allowedGroupCiphers.set(3);
            config.allowedGroupCiphers.set(2);
        } else if (Type == 1) {
            config.wepKeys[0] = "\"" + Password + "\"";
            config.wepTxKeyIndex = 0;
            config.allowedKeyManagement.set(0);
            config.allowedProtocols.set(1);
            config.allowedProtocols.set(0);
            config.allowedAuthAlgorithms.set(0);
            config.allowedAuthAlgorithms.set(1);
            config.allowedPairwiseCiphers.set(2);
            config.allowedPairwiseCiphers.set(1);
            config.allowedGroupCiphers.set(0);
            config.allowedGroupCiphers.set(1);
        } else if (Type == 3 || Type == 4) {
            config.preSharedKey = "\"" + Password + "\"";
            config.allowedProtocols.set(1);
            config.allowedProtocols.set(0);
            config.allowedKeyManagement.set(1);
            config.allowedPairwiseCiphers.set(2);
            config.allowedPairwiseCiphers.set(1);
            config.allowedGroupCiphers.set(0);
            config.allowedGroupCiphers.set(1);
            config.allowedGroupCiphers.set(3);
            config.allowedGroupCiphers.set(2);
        }

        return config;
    }

    public static boolean isWifiOpen(WifiManager manager) {
        return manager != null && manager.isWifiEnabled();
    }

    public static SupplicantState getSupplicantState(WifiManager mMainWifi) {
        WifiInfo wifiInfo = mMainWifi.getConnectionInfo();
        return wifiInfo.getSupplicantState();
    }

    public static String getIpAddress(WifiManager mMainWifi) {
        WifiInfo wifiInfo = mMainWifi.getConnectionInfo();
        return wifiInfo == null ? "" : intToIp(wifiInfo.getIpAddress());
    }

    public static String getIpAddress(Context context) {
        WifiManager mMainWifi = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return getIpAddress(mMainWifi);
    }

    public static String getCurrentSSID(WifiManager wifiManager) {
        return wifiManager == null ? "" : getSSID(wifiManager);
    }

    public static List<WifiConfiguration> getExistConfig(WifiManager wifiManager) {
        return wifiManager == null ? null : wifiManager.getConfiguredNetworks();
    }

    public static List<WifiConfiguration> getExistConfig(Context context) {
        WifiManager wifiManager = getWifiManager(context);
        return wifiManager == null ? null : getExistConfig(wifiManager);
    }

    public static WifiManager getWifiManager(Context context) {
        return context == null ? null : (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    }

    public static WifiConfiguration getCurrentWifiConfig(Context context) {
        String ssid = getCurrentSSID(context);
        List<WifiConfiguration> existingConfig = getExistConfig(context);
        if (existingConfig == null) {
            return null;
        } else {
            int size = existingConfig.size();

            for(int i = size - 1; i >= 0; --i) {
                WifiConfiguration wifiConfiguration = (WifiConfiguration)existingConfig.get(i);
                if (wifiConfiguration.SSID != null && wifiConfiguration.SSID.replace("\"", "").equals(ssid)) {
                    return wifiConfiguration;
                }
            }

            return null;
        }
    }

    public static WifiConfiguration getWifiConfigBySSid(Context context, String ssid) {
        List<WifiConfiguration> existingConfig = getExistConfig(context);
        if (existingConfig == null) {
            return null;
        } else {
            try {
                int size = existingConfig.size();

                for(int i = size - 1; i >= 0; --i) {
                    WifiConfiguration wifiConfiguration = (WifiConfiguration)existingConfig.get(i);
                    if (wifiConfiguration.SSID != null && wifiConfiguration.SSID.replace("\"", "").equals(ssid)) {
                        return wifiConfiguration;
                    }
                }
            } catch (NullPointerException var6) {
                var6.printStackTrace();
            }

            return null;
        }
    }

    public static boolean removeNetwork(Context context, String ssId) {
        WifiConfiguration wifiConfigBySSid = getWifiConfigBySSid(context, ssId);
        if (wifiConfigBySSid == null) {
            return false;
        } else {
            WifiManager wifiManager = getWifiManager(context);
            return wifiManager != null ? wifiManager.removeNetwork(wifiConfigBySSid.networkId) : false;
        }
    }

    public static boolean connectNearNetwork(Context context, List<ScanResult> results) {
        if (context != null && results != null) {
            WifiManager wifiManager = getWifiManager(context);
            List<WifiConfiguration> existConfig = getExistConfig(wifiManager);
            if (existConfig == null) {
                return false;
            } else {
                HashMap<String, Boolean> map = new HashMap();
                Iterator var5 = results.iterator();

                while(var5.hasNext()) {
                    ScanResult scanResult = (ScanResult)var5.next();
                    map.put(scanResult.SSID.replace("\"", ""), true);
                }

                int size = existConfig.size();

                for(int i = size - 1; i >= 0; --i) {
                    WifiConfiguration wifiConfiguration = (WifiConfiguration)existConfig.get(i);
                    String ssid = "";
                    if (wifiConfiguration.SSID != null) {
                        ssid = wifiConfiguration.SSID.replace("\"", "");
                    }

                    Boolean isExists = (Boolean)map.get(ssid);
                    if (isExists != null && isExists) {
                        return enableNetwork(wifiManager, wifiConfiguration.networkId);
                    }
                }

                return false;
            }
        } else {
            return false;
        }
    }

    public static boolean disableNetwork(Context context, String ssId) {
        WifiConfiguration wifiConfigBySSid = getWifiConfigBySSid(context, ssId);
        if (wifiConfigBySSid == null) {
            return false;
        } else {
            WifiManager wifiManager = getWifiManager(context);
            return wifiManager != null ? wifiManager.disableNetwork(wifiConfigBySSid.networkId) : false;
        }
    }

    public static boolean enableNetwork(Context context, String ssId) {
        WifiConfiguration wifiConfigBySSid = getWifiConfigBySSid(context, ssId);
        if (wifiConfigBySSid == null) {
            return false;
        } else {
            WifiManager wifiManager = getWifiManager(context);
            return wifiManager != null ? wifiManager.enableNetwork(wifiConfigBySSid.networkId, true) : false;
        }
    }

    public static boolean enableNetwork(WifiManager wifiManager, int netWorkId) {
        return wifiManager != null ? wifiManager.enableNetwork(netWorkId, true) : false;
    }

    public static List<ScanResult> getScanResultWithoutSsId(List<ScanResult> scanResults, String ssid) {
        if (scanResults == null) {
            return null;
        } else {
            List<ScanResult> resultList = new ArrayList();
            Iterator var3 = scanResults.iterator();

            while(var3.hasNext()) {
                ScanResult scanResult = (ScanResult)var3.next();
                if (!scanResult.SSID.startsWith(ssid)) {
                    resultList.add(scanResult);
                }
            }

            return resultList;
        }
    }

    public static ScanResult getScanResult(List<ScanResult> scanResults, String ssId) {
        if (scanResults == null) {
            return null;
        } else {
            Iterator var2 = scanResults.iterator();

            ScanResult scanResult;
            do {
                if (!var2.hasNext()) {
                    return null;
                }

                scanResult = (ScanResult)var2.next();
            } while(!scanResult.SSID.equals(ssId));

            return scanResult;
        }
    }

    public static boolean isWifiConnected(Context context, String ssId) {
        if (!NetworkUtil.isNetworkAvailable(context)) {
            return false;
        } else {
            String currentSSID = getCurrentSSID(context);
            if (TextUtils.isEmpty(currentSSID)) {
                return false;
            } else {
                return TextUtils.isEmpty(ssId) ? false : ssId.equals(currentSSID);
            }
        }
    }

    public static boolean checkWifiIsInScanResult(List<ScanResult> scanResults, String ssId) {
        if (scanResults == null) {
            return false;
        } else {
            Iterator var2 = scanResults.iterator();

            ScanResult scanResult;
            do {
                if (!var2.hasNext()) {
                    return false;
                }

                scanResult = (ScanResult)var2.next();
            } while(!scanResult.SSID.replace("\"", "").equals(ssId));

            return true;
        }
    }

    public static String getBssid(Context context) {
        WifiManager mWiFiMgr = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (mWiFiMgr == null) {
            return "";
        } else {
            WifiInfo mWiFiInf = mWiFiMgr.getConnectionInfo();
            return mWiFiInf == null ? "" : mWiFiInf.getBSSID();
        }
    }
}